// Comp/list/list.js
Component({
  properties: {
    cid: {
      type: String,
      value: ''
    }
  },

  data: {
    goodsList: []
  },

  observers: {
    'cid': function(cid) {
      console.log('Received cid:', cid);
      if (cid) {
        this.fetchGoodsData(cid);
      }
    }
  },

  ready: function() {
    this.fetchGoodsData(this.properties.cid);
  },

  methods: {
    fetchGoodsData: function(cid) {
      const that = this;
      wx.request({
        url: `https://api-hmugo-web.itheima.net/api/public/v1/goods/search?cid=${cid}`,
        method: 'GET',
        success: function(res) {
          if (res.data.meta.status === 200) {
            that.setData({
              goodsList: res.data.message.goods
            });
          } else {
            console.error('Failed to fetch goods data:', res.data);
          }
        },
        fail: function(err) {
          console.error('Network request failed:', err);
        }
      });
    },
    onGoodsDetail: function(event) {
      const gid = event.currentTarget.dataset.gid;
      console.log('onGoodsDetail gid:', gid);  
      wx.navigateTo({
        url: `/pages/goodsdetails/goodsdetails?gid=${gid}`
      });
    }
  },
});